package b1.scenes.crime
{
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.players.*;
	import b1.scenes.*;
	import b1.scenes.fight.*;
	import b1.scenes.home.*;
	import b1.utils.*;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.*;
	import flash.text.*;
	import flash.ui.Mouse;
	

	public class CrimeItem extends Sprite
	{
		public var playerId:int;
		private	const animalPosY:int = 95;
		private var animalCrimeTip:AnimalCrimeTip;
		
		public function CrimeItem(playerId:int)
		{
			super();
			this.playerId = playerId;
			var playerInfo:PlayerInfo = PlayerInfoHelper.getInstance().players[playerId];
			//背景框
			var TabItemBackground:Class = ResourceHelper.getInstance().getClassByName("TabItemBackground");
			var tabItemBackground:Sprite = new TabItemBackground(); 
			tabItemBackground.width = 125;
			tabItemBackground.height = 125;
			addChild(tabItemBackground);
			//家背景
			tabItemBackground = new TabItemBackground();
			tabItemBackground.width = 125;
			tabItemBackground.height = 125;
			addChild(tabItemBackground);
			var HomeBackGround:Class = ResourceHelper.getInstance().getClassByName("Home" + playerInfo.homeIndex);
			var homeImage:Sprite = new HomeBackGround();
			homeImage.width = tabItemBackground.width;
			homeImage.height = tabItemBackground.height;
			homeImage.alpha = 0.5;
			homeImage.mask = tabItemBackground;
			addChild(homeImage);
			//动物
			var animalMc:AnimalMc;
			if (playerInfo.liveAnimals.length == 1)
			{
				animalMc = new AnimalMc(playerInfo.liveAnimals[0], 75);
				animalMc.x = this.width / 2;
				animalMc.y = animalPosY;
				addChild(animalMc);
			}
			else if (playerInfo.liveAnimals.length > 1)
			{
				for (var i:int = 0; i < 2; i++)
				{
					animalMc = new AnimalMc(playerInfo.liveAnimals[i], 45);
					animalMc.x = this.width * 5 / 16 + this.width * i * 6 / 16;
					animalMc.y = animalPosY;
					addChild(animalMc);
				}
			}
			//标题名字
			var titleText:TextField = new TextField();
			titleText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.LEFT;
			titleText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
			titleText.text = playerInfo.name.substr(0,4);
			titleText.x = (tabItemBackground.width - titleText.width) / 2;
			titleText.y = 95;
			addChild(titleText);
			addEventListener(MouseEvent.CLICK, onCrimeItemClick);
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}
		
		private function onMouseOver(event:MouseEvent):void
		{
			Mouse.cursor = "button";
			this.filters = [new ColorMatrixFilter(
				[1,0,0,0,15,
			     0,1,0,0,15,
			     0,0,1,0,15,
			     0,0,0,1,0 ])];
			animalCrimeTip = new AnimalCrimeTip(playerId);
				
			//加上提示的位置
			if (this.x < 350)
			{
				animalCrimeTip.x = this.x + 280;
			}
			else
			{
				animalCrimeTip.x = this.x + 7;
			}
			animalCrimeTip.y = this.y + 105;
			SceneContainer.getInstance().currentScene.addChild(animalCrimeTip);
		}
		
		private function onMouseOut(event:MouseEvent):void
		{
			Mouse.cursor = "auto";
			this.filters = null;
			
			if (null != animalCrimeTip && null != animalCrimeTip.parent)
			{
				animalCrimeTip.parent.removeChild(animalCrimeTip);
			}
		}
		
		private function onCrimeItemClick(event:MouseEvent):void
		{
			var playerInfo:PlayerInfo = PlayerInfoHelper.getInstance().players[playerId];
			var rightAnimals:Array = PlayerInfoHelper.getInstance().getLiveAnimalsToFight(playerInfo.id);
			var home:Home = SceneContainer.getInstance().currentScene as Home;
			home.startFight(this.playerId, rightAnimals);
		}
	}
}